<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Open an IMAP stream to a mailbox</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.imap-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imap_open</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">imap_open</span> &mdash; <span class="dc-title">Open an <acronym title="Internet Message Access Protocol">IMAP</acronym> stream to a mailbox</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.imap-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imap_open</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$mailbox</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$n_retries</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$params</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ]]] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   Opens an <acronym title="Internet Message Access Protocol">IMAP</acronym> stream to a <code class="parameter">mailbox</code>.
  </p>
  <p class="para">
   This function can also be used to open streams to <acronym title="Post Office Protocol version 3">POP3</acronym> and
   <acronym title="Network News Transfer Protocol">NNTP</acronym> servers, but some functions and features are only
   available on <acronym title="Internet Message Access Protocol">IMAP</acronym> servers.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.imap-open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">mailbox</code></dt>

     <dd>

      <p class="para">
       A mailbox name consists of a server and a mailbox path on this server.
       The special name <em>INBOX</em> stands for the current users
       personal mailbox. Mailbox names that contain international characters
       besides those in the printable ASCII space have to be encoded with
       <span class="function"><a href="imap_utf7_encode.html" class="function">imap_utf7_encode()</a></span>.
      </p>
      <div class="warning"><strong class="warning">Warning</strong><p class="simpara">
Passing untrusted data to this parameter is <em class="emphasis">insecure</em>, unless
<a href="imap.configuration.html#ini.imap.enable-insecure-rsh" class="link">imap.enable_insecure_rsh</a> is disabled.
</p></div>
      <p class="para">
       The server part, which is enclosed in &#039;{&#039; and &#039;}&#039;, consists of the servers
       name or ip address, an optional port (prefixed by &#039;:&#039;), and an optional
       protocol specification (prefixed by &#039;/&#039;). 
      </p>
      <p class="para">
       The server part is mandatory in all mailbox
       parameters. 
      </p>
      <p class="para">
       All names which start with <em>{</em> are remote names, and are
       in the form <em>&quot;{&quot; remote_system_name [&quot;:&quot; port] [flags] &quot;}&quot;
       [mailbox_name]</em> where:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <em>remote_system_name</em> - Internet domain name or
          bracketed IP address of server.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <em>port</em> - optional TCP port number, default is the
          default port for that service
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <em>flags</em> - optional flags, see following table.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <em>mailbox_name</em> - remote mailbox name, default is INBOX
         </span>
        </li>
       </ul>
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>Optional flags for names</strong></caption>
        
         <thead>
          <tr>
           <th>Flag</th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><em>/service=</em><em class="emphasis">service</em></td>
           <td>mailbox access service, default is &quot;imap&quot;</td>
          </tr>

          <tr>
           <td><em>/user=</em><em class="emphasis">user</em></td>
           <td>remote user name for login on the server</td>
          </tr>

          <tr>
           <td><em>/authuser=</em><em class="emphasis">user</em></td>
           <td>remote authentication user; if specified this is the user name
            whose password is used (e.g. administrator)</td>
          </tr>

          <tr>
           <td><em>/anonymous</em></td>
           <td>remote access as anonymous user</td>
          </tr>

          <tr>
           <td><em>/debug</em></td>
           <td>record protocol telemetry in application&#039;s debug log</td>
          </tr>

          <tr>
           <td><em>/secure</em></td>
           <td>do not transmit a plaintext password over the network</td>
          </tr>

          <tr>
           <td><em>/imap</em>, <em>/imap2</em>,
            <em>/imap2bis</em>, <em>/imap4</em>,
            <em>/imap4rev1</em></td>
           <td>equivalent to <em>/service=imap</em></td>
          </tr>

          <tr>
           <td><em>/pop3</em></td>
           <td>equivalent to <em>/service=pop3</em></td>
          </tr>

          <tr>
           <td><em>/nntp</em></td>
           <td>equivalent to <em>/service=nntp</em></td>
          </tr>

          <tr>
           <td><em>/norsh</em></td>
           <td>do not use rsh or ssh to establish a preauthenticated IMAP
            session</td>
          </tr>

          <tr>
           <td><em>/ssl</em></td>
           <td>use the <em>Secure Socket Layer</em> to encrypt the session</td>
          </tr>

          <tr>
           <td><em>/validate-cert</em></td>
           <td>validate certificates from TLS/SSL server (this is the default
            behavior)</td>
          </tr>

          <tr>
           <td><em>/novalidate-cert</em></td>
           <td>do not validate certificates from TLS/SSL server, needed if
            server uses self-signed certificates</td>
          </tr>

          <tr>
           <td><em>/tls</em></td>
           <td>force use of <em>start-TLS</em> to encrypt the session, and reject
            connection to servers that do not support it</td>
          </tr>

          <tr>
           <td><em>/notls</em></td>
           <td>do not do <em>start-TLS</em> to encrypt the session, even with servers
            that support it</td>
          </tr>

          <tr>
           <td><em>/readonly</em></td>
           <td>request read-only mailbox open (IMAP only; ignored on NNTP, and
            an error with SMTP and POP3)</td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">username</code></dt>

     <dd>

      <p class="para">
       The user name
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">password</code></dt>

     <dd>

      <p class="para">
       The password associated with the <code class="parameter">username</code>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       The <code class="parameter">options</code> are a bit mask with one or more of
       the following:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_READONLY</code></strong> - Open mailbox read-only
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_ANONYMOUS</code></strong> - Don&#039;t use or update a
          <var class="filename">.newsrc</var> for news (NNTP only)
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_HALFOPEN</code></strong> - For <acronym title="Internet Message Access Protocol">IMAP</acronym>
          and <acronym title="Network News Transfer Protocol">NNTP</acronym> names, open a connection but
          don&#039;t open a mailbox.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>CL_EXPUNGE</code></strong> - Expunge mailbox automatically upon mailbox close
          (see also <span class="function"><a href="imap_delete.html" class="function">imap_delete()</a></span> and
          <span class="function"><a href="imap_expunge.html" class="function">imap_expunge()</a></span>)
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_DEBUG</code></strong> - Debug protocol negotiations
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_SHORTCACHE</code></strong> - Short (<em>elt</em>-only) caching
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_SILENT</code></strong> - Don&#039;t pass up events (internal use)
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_PROTOTYPE</code></strong> - Return driver prototype
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>OP_SECURE</code></strong> - Don&#039;t do non-secure authentication
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">n_retries</code></dt>

     <dd>

      <p class="para">
       Number of maximum connect attempts
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">params</code></dt>

     <dd>

      <p class="para">
       Connection parameters, the following (string) keys maybe used 
       to set one or more connection parameters:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <em>DISABLE_AUTHENTICATOR</em> - Disable authentication properties
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.imap-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns an IMAP stream on success or <strong><code>FALSE</code></strong> on error.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.imap-open-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.2</td>
       <td><code class="parameter">params</code> added</td>
      </tr>

      <tr>
       <td>5.2.0</td>
       <td><code class="parameter">n_retries</code> added</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.imap-open-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4030">
    <p><strong>Example #1 Different use of <span class="function"><strong>imap_open()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;To&nbsp;connect&nbsp;to&nbsp;an&nbsp;IMAP&nbsp;server&nbsp;running&nbsp;on&nbsp;port&nbsp;143&nbsp;on&nbsp;the&nbsp;local&nbsp;machine,<br />//&nbsp;do&nbsp;the&nbsp;following:<br /></span><span style="color: #0000BB">$mbox&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_open</span><span style="color: #007700">(</span><span style="color: #DD0000">"{localhost:143}INBOX"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;To&nbsp;connect&nbsp;to&nbsp;a&nbsp;POP3&nbsp;server&nbsp;on&nbsp;port&nbsp;110&nbsp;on&nbsp;the&nbsp;local&nbsp;server,&nbsp;use:<br /></span><span style="color: #0000BB">$mbox&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_open&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"{localhost:110/pop3}INBOX"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;To&nbsp;connect&nbsp;to&nbsp;an&nbsp;SSL&nbsp;IMAP&nbsp;or&nbsp;POP3&nbsp;server,&nbsp;add&nbsp;/ssl&nbsp;after&nbsp;the&nbsp;protocol<br />//&nbsp;specification:<br /></span><span style="color: #0000BB">$mbox&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_open&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"{localhost:993/imap/ssl}INBOX"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;To&nbsp;connect&nbsp;to&nbsp;an&nbsp;SSL&nbsp;IMAP&nbsp;or&nbsp;POP3&nbsp;server&nbsp;with&nbsp;a&nbsp;self-signed&nbsp;certificate,<br />//&nbsp;add&nbsp;/ssl/novalidate-cert&nbsp;after&nbsp;the&nbsp;protocol&nbsp;specification:<br /></span><span style="color: #0000BB">$mbox&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_open&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"{localhost:995/pop3/ssl/novalidate-cert}"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;To&nbsp;connect&nbsp;to&nbsp;an&nbsp;NNTP&nbsp;server&nbsp;on&nbsp;port&nbsp;119&nbsp;on&nbsp;the&nbsp;local&nbsp;server,&nbsp;use:<br /></span><span style="color: #0000BB">$nntp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_open&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"{localhost:119/nntp}comp.test"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;To&nbsp;connect&nbsp;to&nbsp;a&nbsp;remote&nbsp;server&nbsp;replace&nbsp;"localhost"&nbsp;with&nbsp;the&nbsp;name&nbsp;or&nbsp;the<br />//&nbsp;IP&nbsp;address&nbsp;of&nbsp;the&nbsp;server&nbsp;you&nbsp;want&nbsp;to&nbsp;connect&nbsp;to.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   <div class="example" id="example-4031">
    <p><strong>Example #2 <span class="function"><strong>imap_open()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$mbox&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_open</span><span style="color: #007700">(</span><span style="color: #DD0000">"{imap.example.org:143}"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"username"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h1&gt;Mailboxes&lt;/h1&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$folders&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_listmailbox</span><span style="color: #007700">(</span><span style="color: #0000BB">$mbox</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"{imap.example.org:143}"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"*"</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$folders&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Call&nbsp;failed&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$folders&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h1&gt;Headers&nbsp;in&nbsp;INBOX&lt;/h1&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$headers&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imap_headers</span><span style="color: #007700">(</span><span style="color: #0000BB">$mbox</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$headers&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Call&nbsp;failed&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$headers&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">imap_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$mbox</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.imap-open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="imap_close.html" class="function" rel="rdfs-seeAlso">imap_close()</a> - Close an IMAP stream</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>